package core.actions;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import core.GameOptions;

/**
 * 
 * @author Anton
 * Defines all usable actions in the game.
 * Need one instance for each player
 */
public class ActionSet {
	private List<ActionInfo> actionInfoList = new ArrayList<ActionInfo>();
	private List<Action> actions = new ArrayList<Action>();
	
	public ActionSet(String modName){
		actions.add(new FireLaser());
		actions.add(new FireMissile());
		actions.add(new FireMortar());
		actions.add(new FireShotgun());
		actions.add(new MoveHero());
		actions.add(new OrderAllMove());
		actions.add(new SpawnArtilleryBuggy());
		actions.add(new SpawnDefaultUnit());
		actions.add(new FireSniper());
		actions.add(new NextUnit());
		actions.add(new AddMachinegun());
		try{
			Scanner sc = new Scanner(new FileReader(GameOptions.modFolder+modName+"linkmap.txt"));
			while(sc.hasNext()){
				Scanner fin = new Scanner(sc.nextLine());
				Action ac = findAction(fin.next());
				List<String> toBeLinked = new ArrayList<String>();
				while(fin.hasNext()){
					toBeLinked.add(fin.next());
				}
				actionInfoList.add(new ActionInfo(ac, toBeLinked));
			}
			linkActionInfo();
			for(ActionInfo ai : actionInfoList){
				System.out.println(ai);
			}		
		}
		catch(FileNotFoundException ex){
			System.out.println("Warning! mod file not found");
		}
	}
	
	public ActionSet(){
		actions.add(new FireLaser());
		actions.add(new FireMissile());
		actions.add(new FireMortar());
		actions.add(new FireShotgun());
		actions.add(new MoveHero());
		actions.add(new OrderAllMove());
		actions.add(new SpawnArtilleryBuggy());
		actions.add(new SpawnDefaultUnit());
		actions.add(new FireSniper());
		actions.add(new NextUnit());
		
		// Laser Missile Mortar Shotgun Move Move_units Spawn_buggy Spawn_unit
		String[] linkMap = {"Laser Shotgun Missile Move",
							"Move Move_units Laser Shotgun",
							"Shotgun Move Laser Mortar",
							"Mortar Shotgun Move",
							"Missile Laser",
							"Move_units Spawn_buggy Spawn_unit Move Mortar",
							"Spawn_buggy Spawn_unit Move_units",
							"Spawn_unit Spawn_buggy Move_units"};
		
		
		for (String s : linkMap){
			Scanner fin = new Scanner(s);
			Action ac = findAction(fin.next());
			List<String> toBeLinked = new ArrayList<String>();
			while(fin.hasNext()){
				toBeLinked.add(fin.next());
			}
			actionInfoList.add(new ActionInfo(ac, toBeLinked));
		}
		linkActionInfo();
		for(ActionInfo ai : actionInfoList){
			System.out.println(ai);
		}
		
	}
	
	
	
	
	/**
	 * Call after all actions
	 */
	private void linkActionInfo(){
		for(ActionInfo ai : actionInfoList){
			List<ActionInfo> linkedActions = new ArrayList<ActionInfo>();
			for(String name : ai.getActionsToBeLinked()){
				linkedActions.add(findActionInfo(name));
			}
			ai.setLinkedActions(linkedActions);
		}
	}
	private Action findAction(String name){
		for(Action a : actions){
			if(a.toString().equalsIgnoreCase(name))
				return a;
		}
		System.out.println("WARNING: Action "+name+" not found. Misspelled action name");
		return null;
	}
	public ActionInfo findActionInfo(String name){
		for(ActionInfo ai : actionInfoList){
			if(ai.getAction().toString().equalsIgnoreCase(name))
				return ai;
		}
		System.out.println("WARNING: Action "+name+" not found. Misspelled action name");
		return null;
	}
}
